/*
 *http://www.apache.org/licenses/LICENSE-2.0
 *Copyright (c) 2021 fullset
 *@ClassName SysUserServiceImpl
 *@date 2021/5/29 15:38
 *@Description TODO
 */
package com.fs.base.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.fs.base.service.SysUserService;
import com.fs.core.exception.BizException;
import com.fs.core.exception.BizExceptionEnum;
import com.fs.core.util.BeanUtil;
import com.fs.orm.dao.SysUserDao;
import com.fs.orm.data.XmSelectData;
import com.fs.orm.entity.SysUser;
import com.fs.orm.vo.form.SysUserForm;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
public class SysUserServiceImpl implements SysUserService {

    @Autowired
    private SysUserDao sysUserDao;

    @Override
    public SysUserForm addUser(SysUserForm sysUserForm) throws Exception {
        SysUser sysUser = new SysUser();
        BeanUtil.copyAttribute(sysUserForm, sysUser);
        boolean result;
        try {
            result = sysUserDao.addUser(sysUser);

        }catch (Exception e){
            throw new Exception("sql语法错误!");
        }
        if(!result){
            throw new BizException(BizExceptionEnum.SYS_USER_ADD_ERROR);
        }
        return sysUserForm;
    }

    @Override
    public SysUserForm editUser(SysUserForm sysUserForm) {
        SysUser sysUser = new SysUser();
        BeanUtil.copyAttribute(sysUserForm, sysUser);
        try {
            sysUser = sysUserDao.editUser(sysUser);

        }catch (Exception e){
            // throw new Exception("sql语法错误!");
        }
        return sysUserForm;
    }

    @Override
    public List<SysUser> getAll() {
        QueryWrapper qWrapper = new QueryWrapper();
        qWrapper.eq("is_deleted", 0);
        return sysUserDao.getList(qWrapper);
    }

    @Override
    public PageInfo<SysUser> getLayTable(Map<String, Object> params) {
        PageHelper.startPage(Integer.valueOf(String.valueOf(params.get("page"))),Integer.valueOf(String.valueOf(params.get("limit")))) ;
        QueryWrapper qWrapper = new QueryWrapper();
        if(null != params.get("userName")){
            qWrapper.likeLeft("user_name", String.valueOf(params.get("userName")));
        }
        qWrapper.orderByAsc("id");
        List<SysUser> list = sysUserDao.getList(qWrapper);
        PageInfo<SysUser> userPage =new PageInfo<>(list);
        return userPage;
    }

    @Override
    public SysUserForm initForm() {
        SysUserForm sysUserForm = new SysUserForm();
        sysUserForm.setUserStateList(XmSelectData.getUserState(sysUserForm.getUserState()));
        sysUserForm.setSexList(XmSelectData.getSex(sysUserForm.getSex()));
        return sysUserForm;
    }

    @Override
    public SysUserForm editForm(Long id) {
        SysUser sysUser = sysUserDao.getById(id);
        SysUserForm sysUserForm = new SysUserForm();
        BeanUtil.copyAttribute(sysUser, sysUserForm);
        sysUserForm.setUserStateList(XmSelectData.getUserState(sysUserForm.getUserState()));
        sysUserForm.setSexList(XmSelectData.getSex(sysUserForm.getSex()));
        return sysUserForm;
    }

    @Override
    public boolean logicalDeleteById(String id) {
        return sysUserDao.logicalDeleteById(id);
    }

    @Override
    public boolean batchDel(String ids) {
        return sysUserDao.batchDel(ids);
    }
}
